// pages/test1/test1.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'

Page({
  data: {
    isShowCamera: false,
    imgIp: getApp().globalData.imgIp,
    sessionid: '',
  },

  takePhotoAction() {
    if (getApp().globalData.sessionid === '') {
      Toast('登录后才能使用哦~')
    } else {
      const ctx = wx.createCameraContext()
      ctx.takePhoto({
        quality: 'high', //高质量
        success: (res) => {
          wx.navigateTo({
            url:
              '../../pages/judge/judge?img_path=' +
              res.tempImagePath +
              '&tabbar=' +
              1,
          })
        },
      })
    }
  },
  onLoad: function (options) {},

  onShow: function () {
    var that = this
    wx.authorize({
      scope: 'scope.camera',
      success: function (res) {
        that.setData({
          isShowCamera: true,
        })
      },
      fail: function (res) {
        // console.log('' + res)
        Dialog.confirm({
          title: '请求授权您的摄像头',
          message:
            '如需正常使用此小程序功能，请您按确认并在设置页面授权用户信息',
        })
          .then(() => {
            wx.openSetting({
              success: function (res) {
                if (res.authSetting['scope.camera']) {
                  //设置允许获取摄像头
                  console.log('设置允许获取摄像头')
                  Toast.success('授权成功')
                  that.setData({
                    isShowCamera: true,
                  })
                } else {
                  //不允许
                  Toast.fail('授权失败')
                }
              },
            })
          })
          .catch(() => {
            Toast.fail('授权失败')
          })
      },
    })
    that.setData({
      sessionid: getApp().globalData.sessionid || '',
    })
  },
})
